﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using BusinessObjects.BO_GetObjects;
using BussinessObjects;
using BusinessObjects;
using LibraryO2S;

public partial class controls_admin_News : Pages_Base
{
    private DataTable objDT;
    protected void Page_Load(object sender, EventArgs e)
    {
        base.removeHGC(divMessage);
        base.removeHGC(divResult);
        if (!IsPostBack)
        {
            try
            {
                base.loadCategories(ddlCategory, 1);
                if (Request["c_id"] != null)
                    ddlCategory.SelectedValue = Request["c_id"].ToString();
                loadData();
            }
            catch (Exception ex)
            {
                divMessage.InnerHtml = O2S_Message.Error("Lỗi", "Có lỗi khi truy cập hệ thống! <br>" + ex.Message);
            }
            finally { base.CloseConnection(); }
        }
    }
    protected void loadData()
    {
        try
        {
            if (Request["m_id"] != null)
            {
                objDT = BO_Articles.SelectByModule(Convert.ToInt32(Request["m_id"]), base.Cur_page, base.Page_size);
                bindRepeater(rptNews, objDT, pagerContainer, divMessage);
            }
            else
            {
                update.Visible = false;
                int c_id = Convert.ToInt32(ddlCategory.SelectedValue);
                DataTable categoryDT = BO_Categories.Search(c_id);
                if (categoryDT != null && categoryDT.Rows.Count > 0)
                    title.InnerText = "Danh Sách " + categoryDT.Rows[0]["FULL_CATEGORY_NAME"].ToString();
                if (Request["k_w"] != null)
                    objDT = BO_Articles.SearchLike(Request["k_w"].ToString(), 1, c_id, base.Page_size, base.Cur_page);
                else
                    objDT = BO_Articles.SelectByCategory(c_id, base.Cur_page, base.Page_size);
                if (!bindRepeater(rptNews, objDT, pagerContainer, divMessage))
                    if (Request["k_w"] != null)
                        divMessage.InnerHtml = O2S_Message.Warning("Hệ Thống", "Không có bản ghi nào phù hợp với từ khóa: " + Request["k_w"].ToString() + "!");
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
    protected void rptNews_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        try
        {
            base.OpenConnection();
            Button btnID = (Button)e.Item.FindControl("btnID");
            int a_id = Convert.ToInt32(btnID.CommandArgument);
            string commandName = e.CommandName;
            if (commandName.Equals("del"))
            {
                BO_Articles.Delete(a_id);
                divMessage.InnerHtml = O2S_Message.Success("Thông Báo", "Xóa thành công .");
            }
            else if (commandName.Equals("Display"))
                if (Request["m_id"] != null)
                    BO_Articles.IsHot(a_id, Convert.ToInt32(Request["m_id"]));
                else
                    BO_Articles.IsHome(a_id);
            loadData();
        }
        catch (Exception ex)
        {
            divMessage.InnerHtml = O2S_Message.Error("Lỗi", "Phát hiện lỗi: <br />" + ex.Message);
        }
        finally { base.CloseConnection(); }
    }
    protected void rptNews_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (Request["m_id"] == null)
        {
            Button btnDisplay = (Button)e.Item.FindControl("btnDisplay");
            if (btnDisplay != null)
                if (!Convert.ToBoolean(btnDisplay.CommandArgument))
                    btnDisplay.CssClass = "icon-32-cancel";
                else
                    btnDisplay.CssClass = "icon-32-checking";
        }
    }
    protected void btnNew_Click(object sender, EventArgs e)
    {
        Response.Redirect("Default.aspx?module=newsadd");
    }
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        string keyword = txtSearch.Value.Trim();
        if (keyword != null && !keyword.Equals("") && !keyword.Equals("Hãy nhập từ khóa !"))
            Response.Redirect("default.aspx?module=news&k_w=" + keyword);
    }
    protected void ddlCategory_SelectedIndexChanged(object sender, EventArgs e)
    {
        string url = base.removeRequest();
        if (Request["c_id"] != null)
        {
            int conti = url.LastIndexOf("c_id=");
            string start = url.Substring(0, url.LastIndexOf("&c_id="));
            string end = url.Substring(conti, url.Length - conti);
            conti = end.IndexOf("&");
            if (conti > -1)
                url = start + end.Substring(conti, end.Length - conti);
            else
                url = start;
        }
        Response.Redirect(url + "&c_id=" + ddlCategory.SelectedValue);
    }
}
